package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.domain.system.EvsUser;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.domain.system.SysUserRole;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.exception.BusinessException;
import com.wejoy.video.middle.station.data.mapper.system.ISysUserRoleMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.IEvsUserService;
import com.wejoy.video.middle.station.data.service.system.ISysUserRoleService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.system.AddRoleVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统--用户角色关联表
 *
 * @author admin
 * @date 2021年10月19日 15:06:59
 */
@Service
public class SysUserRoleServiceImpl implements ISysUserRoleService {
    @Autowired
    private ISysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private IIdWorkerService idWorkerService;
    @Autowired
    private IEvsUserService evsUserService;

    @Override
    public void save(SysUserRole req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysUserRole req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return sysUserRoleMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(SysUserRole sysUserRole) {
        WJAssert.notNull(sysUserRole, "入参对象不能为空");
        WJAssert.notNull(sysUserRole.getId(), "id参数错误");
        SysUserRole where = new SysUserRole();
        where.setId(sysUserRole.getId());
        return sysUserRoleMapper.updateSelective(sysUserRole, where);
    }

    @Override
    public int getCount(SysUserRole sysUserRole) {
        checkStringLength(sysUserRole, true);
        return sysUserRoleMapper.getCount(sysUserRole);
    }

    @Override
    public SysUserRole getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysUserRole where = new SysUserRole();
        where.setId(id);
        return sysUserRoleMapper.getOne(where);
    }

    @Override
    public List<SysUserRole> getList(SysUserRole sysUserRole) {
        checkStringLength(sysUserRole, true);
        return sysUserRoleMapper.getList(sysUserRole);
    }

    @Override
    public List<SysUserRole> getPageList(SysUserRole sysUserRole) {
        checkStringLength(sysUserRole, true);
        return sysUserRoleMapper.getPageList(sysUserRole);
    }

    @Override
    public void delete(Long id, String operator) {
        SysUserRole req = new SysUserRole();
        req.setId(id);
        req.setOperator(operator);
        updateSelectiveById(req);
    }

    /**
     * 为用户分配角色
     *
     * @param addRoleVO
     * @param currentUser
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRole(AddRoleVO addRoleVO, SysUser currentUser) {
        List<Long> newRoleList = addRoleVO.getNewRole();
        if (null == addRoleVO.getUserId()) {
            throw new BusinessException("新增角色ID或用户ID不能为空");
        }
        sysUserRoleMapper.deleteByUserId(addRoleVO.getUserId());
        String roleId="";
        for (Long l : newRoleList) {
            roleId=roleId+l+",";
            SysUserRole req = new SysUserRole();
            req.setSysRoleId(l);
            req.setOperator(currentUser.getName());
            req.setSysUserId(addRoleVO.getUserId());
            insertSelective(req);
        }
        evsUserService.updateRoleSelectiveById(addRoleVO.getUserId(),roleId);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysUserRole sysUserRole, boolean nullAble) {
        WJAssert.notNull(sysUserRole, "入参对象不能为空");
        WJAssert.limitMaxLength(sysUserRole.getOperator(), nullAble, 50, "执行者不合法!");
        WJAssert.limitMaxLength(sysUserRole.getRemark(), true, 255, "备注不合法!");
    }
}
